package com.ylt.lock8;

import java.util.concurrent.TimeUnit;

/**
 * 3.增加一个普通方法，先执行hello再执行发短信
 * 4.两个对象，两个同步方法，先电话再发短信
 */

public class Test02 {
    public static void main(String[] args) {
        //两个对象
        Phone02 phone01 = new Phone02();
        Phone02 phone02 = new Phone02();
        new Thread(() -> {
            phone01.sendSms();
        }, "A").start();

        //捕获异常
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        new Thread(() -> {
            phone02.call();
        }, "B").start();

    }
}

class Phone02 {

    //锁的对象是方法调用者
    //两个方法用同一个锁，谁先拿到谁先执行
    public synchronized void sendSms() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("发短信");
    }

    public synchronized void call() {
        System.out.println("打电话");
    }

    public void hello(){
        System.out.println("hello");
    }
}
